#include "mylist.h"

#if 0
static void event_cb(lv_event_t * e)
{
    /*The original target of the event. Can be the buttons or the container*/
    lv_obj_t * target = lv_event_get_target(e);

    /*The current target is always the container as the event is added to it*/
    lv_obj_t * list = lv_event_get_current_target(e);

    /*If container was clicked do nothing*/
    if(target == list) return;

    /*Make the clicked buttons red*/
    lv_obj_set_style_bg_color(target, lv_palette_main(LV_PALETTE_RED), 0);
    printf("%s\n", lv_list_get_btn_text(list, target)); //取得当前点击的按钮文本
}

static void list_1(void)
{
    //1.创建列表
    lv_obj_t* list = lv_list_create(lv_scr_act());
    lv_obj_set_size(list, 200, 400);
    lv_obj_set_align(list, LV_ALIGN_CENTER);
    //2.添加列表文本
    lv_list_add_text(list, "Settings");

    // 添加列表选项
    lv_obj_t* btn1 = lv_list_add_btn(list, LV_SYMBOL_WIFI, "WIFI");
    lv_obj_t* btn2 = lv_list_add_btn(list, LV_SYMBOL_WIFI, "Blue");
    lv_obj_t* btn3 = lv_list_add_btn(list, LV_SYMBOL_WIFI, "A");
    lv_obj_t* btn4 = lv_list_add_btn(list, LV_SYMBOL_WIFI, "B");
    lv_obj_t* btn5 = lv_list_add_btn(list, LV_SYMBOL_WIFI, "C");

    // 添加冒泡标志（子控件事件会传递给父控件）
    lv_obj_add_flag(btn1, LV_OBJ_FLAG_EVENT_BUBBLE);
    lv_obj_add_flag(btn2, LV_OBJ_FLAG_EVENT_BUBBLE);
    lv_obj_add_flag(btn3, LV_OBJ_FLAG_EVENT_BUBBLE);
    lv_obj_add_flag(btn4, LV_OBJ_FLAG_EVENT_BUBBLE);
    lv_obj_add_flag(btn5, LV_OBJ_FLAG_EVENT_BUBBLE);

    lv_obj_add_event_cb(list, event_cb, LV_EVENT_CLICKED, NULL);

}
#endif


lv_obj_t* label;
lv_obj_t* list;
lv_obj_t* btn1;
lv_obj_t* btn2;
lv_obj_t* btn3;
lv_obj_t* btn4;

static void event_cb(lv_event_t* e)
{
    lv_obj_t* target = lv_event_get_target(e);
    lv_label_set_text(label, lv_list_get_btn_text(list, target));
    
    //选中后此栏变蓝
    lv_obj_add_state(target, LV_STATE_FOCUS_KEY);
}

//主程序
static void list_2(void)
{
    //标签
    label = lv_label_create(lv_scr_act());
    lv_label_set_text(label, "0");
    lv_obj_set_style_text_font(label, &lv_font_montserrat_20, LV_PART_MAIN);
    lv_obj_set_align(label, LV_ALIGN_RIGHT_MID);

    //1.创建列表
    list = lv_list_create(lv_scr_act());
    lv_obj_set_size(list, 200, 400);
    lv_obj_set_align(list, LV_ALIGN_CENTER);

    //2.添加列表文本
    lv_list_add_text(list, "Settings1");
    btn1 = lv_list_add_btn(list, LV_SYMBOL_WIFI, "WIFI");
    lv_obj_add_event_cb(btn1, event_cb, LV_EVENT_CLICKED, NULL);
    btn2 = lv_list_add_btn(list, LV_SYMBOL_FILE, "FILE");
    lv_obj_add_event_cb(btn2, event_cb, LV_EVENT_CLICKED, NULL);

    lv_list_add_text(list, "Settings2");
    btn3 = lv_list_add_btn(list, LV_SYMBOL_GPS, "GPS");
    lv_obj_add_event_cb(btn3, event_cb, LV_EVENT_CLICKED, NULL);
    btn4 = lv_list_add_btn(list, LV_SYMBOL_KEYBOARD, "KEYBOARD");
    lv_obj_add_event_cb(btn4, event_cb, LV_EVENT_CLICKED, NULL);
}


void mylist_example(void)
{
    list_2();
}